﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SongSharp.Domain.Objects;
using SongSharp.Domain.Suggesting.VaryVogueValue;
using SongSharp.Logging;

namespace SongSharp.Domain.Suggesting
{
    public class SuggestionManager
    {
        private static SuggestionManager m_instance;
        private Random RandomGenerator { get; set; }
        public RulesManager Rules { get; private set; }

        public Weight Weight { get; set; }

        public ChanceGenerator VitalGen { get; private set; }
        public ChanceGenerator VistaGen { get; private set; }
        public ChanceGenerator VoteGen { get; private set; }

        private SuggestionManager(Weight weight)
        {
            RandomGenerator = new Random();
            Rules = RulesManager.Instance;
            Weight = weight;

            VitalGen = new VitalGenerator();
            VistaGen = new VistaGenerator();
            VoteGen = new VoteGenerator();
        }

        public static SuggestionManager CreateInstance(Weight weight)
        {
            if (m_instance == null)
                    m_instance = new SuggestionManager(weight);
                return m_instance;
        }

        public double GenerateWeightedRandom()
        {
            return Math.Pow(RandomGenerator.NextDouble(), (9-Weight)/10);
        }

        public ChanceValue GenerateVaryVogueValue(Track track)
        {
            Logger.Log("#: " + System.IO.Path.GetFileName(track.Url) + ", ID: " + track.Uid);

            double random = GenerateWeightedRandom();
            ChanceValue vital = VitalGen.Generate(track);
            ChanceValue vista = VistaGen.Generate(track);

            ////ChanceValue vote = VoteGen.Generate(track);

            ChanceValue v = vital + vista;

            Logger.Log("   Addition of [" + Math.Round(vital.Value, 4) + ";" + vital.DegreeOfBelief
                + "] + [" + Math.Round(vista.Value, 4) + ";" + vista.DegreeOfBelief + "] = ["
                + Math.Round(v.Value, 4) + "]");
            
            return v;
        }
    }
}
